﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using DevExpress.Xpo;
using Zero.Core.Domain;
using Zero.Core.Domain.Query;
using Zero.POCO.Donnees;

namespace Zero.DataLayer.Repository
{
    public class IndividuRepository:IRepository<Individu,Guid>
    {

        #region Implementation

        
        public Individu FindBy(Guid id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Individu> FindAll()
        {
            List<Individu> Modelindiv;
            var config = new AppSettingsReader();
            using (
                   var uow = new UnitOfWork()
                   {
                       ConnectionString = ((string)config.GetValue("connect", typeof(string)))
                   })
            {
                var individus = new XPCollection<Zero.DataLayer.Donnees.Individu>(uow);
                Modelindiv = new List<Individu>();
                foreach (var individu in individus)
                {
                    Modelindiv.Add(MAP.MapInverse.MapIndividus(individu));
                }
            }

            return Modelindiv;
        }

        public IEnumerable<Individu> FindBy(Query query)
        {
            List<Individu> Modelindiv;
            var firstOrDefault = query.Criteria.FirstOrDefault();
            if(firstOrDefault != null && firstOrDefault.criteriaOperator==CriteriaOperator.Equal)
            {

                var config = new AppSettingsReader();
                using (
                       var uow = new UnitOfWork()
                       {
                           ConnectionString = ((string)config.GetValue("connect", typeof(string)))
                       })
                {
                    var individus = new XPCollection<Zero.DataLayer.Donnees.Individu>(uow);
                    var Xpoindiv = individus.Where(p =>
                    {
                        var orDefault = query.Criteria.FirstOrDefault();
                        return orDefault != null && p.IdQuest == orDefault.Value.ToString();
                    });
                    Modelindiv=new List<Individu>();
                    foreach (var individu in Xpoindiv)
                    {
                        Modelindiv.Add(MAP.MapInverse.MapIndividus(individu));
                    }
                }

                return Modelindiv;
            }
            return null;
        }

        public IEnumerable<Individu> FindBy(Query query, int index, int count)
        {
            throw new NotImplementedException();
        }

        public void Save(Individu entity)
        {
            var config = new AppSettingsReader();
            using (
                   var uow = new UnitOfWork()
                   {
                       ConnectionString = ((string)config.GetValue("connect", typeof(string)))
                   })
            {
                var individu=MAP.Map.MapIndividu(entity, uow);
                uow.CommitChanges();
            }
        }


        public void SaveAll(IEnumerable<Individu> entities)
        {
            var config = new AppSettingsReader();
            using (
                   var uow = new UnitOfWork()
                   {
                       ConnectionString = ((string)config.GetValue("connect", typeof(string)))
                   })
            {
                Donnees.Individu individu;
                foreach (var entity in entities)
                {
                    individu = MAP.Map.MapIndividu(entity, uow);
                }
                
                uow.CommitChanges();
            }
        }

        public List<Individu> FindAll(List<Query> queries)
        {

            List<Individu> Modelindiv = new List<Individu>();
            var config = new AppSettingsReader();
            using (
                   var uow = new UnitOfWork()
                   {
                       ConnectionString = ((string)config.GetValue("connect", typeof(string)))
                   })
            {
                foreach (var query in queries)
                {
                    var individus = new XPCollection<Zero.DataLayer.Donnees.Individu>(uow);
                    var Xpoindiv = individus.Where(p => p.IdQuest == query.Criteria.FirstOrDefault().Value.ToString());
                  
                    foreach (var individu in Xpoindiv)
                    {
                        Modelindiv.Add(MAP.MapInverse.MapIndividus(individu));
                    }
                }


                
            }
            return Modelindiv;

        }

        

        public void Add(Individu entity)
        {
            throw new NotImplementedException();
        }

        public void Remove(Individu entity)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
